package com.ruoyi.safe.service.impl;

import com.ruoyi.common.config.MinIoProperties;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.NameUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.MinIoUtil;
import com.ruoyi.safe.domain.BizEducateDocAttach;
import com.ruoyi.safe.domain.vo.BizEducateDocAttachVO;
import com.ruoyi.safe.domain.vo.BizSafeDocAttachVO;
import com.ruoyi.safe.mapper.BizEducateDocAttachMapper;
import com.ruoyi.safe.service.IBizEducateDocAttachService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 教育资料文件Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-15
 */
@Service
public class BizEducateDocAttachServiceImpl implements IBizEducateDocAttachService 
{
    @Autowired
    BizEducateDocAttachMapper bizEducateDocAttachMapper;
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    MinIoProperties minIoProperties;

    /**
     * 查询教育资料文件
     * 
     * @param id 教育资料文件主键
     * @return 教育资料文件
     */
    @Override
    public BizEducateDocAttach selectBizEducateDocAttachById(Long id)
    {
        return bizEducateDocAttachMapper.selectBizEducateDocAttachById(id);
    }

    @Override
    public List<BizEducateDocAttachVO> selectBizEducateDocAttachByIds(Long[] ids) {
        if(ids.length == 0) return new ArrayList<>();
        List<BizEducateDocAttach> bizEducateDocAttaches = bizEducateDocAttachMapper.selectBizEducateDocAttachByIds(ids);
        Set<String> userNames = bizEducateDocAttaches.stream().map(item->item.getCreateBy()).collect(Collectors.toSet());
        List<BizEducateDocAttachVO> bizEducateDocAttachVOS = bizEducateDocAttaches.stream().map(item->{
            BizEducateDocAttachVO bizEducateDocAttachVO = new BizEducateDocAttachVO();
            BeanUtils.copyProperties(item,bizEducateDocAttachVO);
            bizEducateDocAttachVO.setUrl(MinIoUtil.getFileUrl(minIoProperties.getBucketEdu(),bizEducateDocAttachVO.getRealName()));
            bizEducateDocAttachVO.setFileId(String.valueOf(bizEducateDocAttachVO.getId()));
            return bizEducateDocAttachVO;
        }).collect(Collectors.toList());
        if(userNames.size() > 0){
            SysUser sysUser = new SysUser();
            sysUser.setUserNames(userNames);
            List<SysUser> sysUsers = sysUserMapper.selectUserList(sysUser);
            NameUtils.handleUserNameToNickName(bizEducateDocAttachVOS,sysUsers,"createBy");
        }
        return bizEducateDocAttachVOS;
    }

    /**
     * 查询教育资料文件列表
     * 
     * @param bizEducateDocAttach 教育资料文件
     * @return 教育资料文件
     */
    @Override
    public List<BizEducateDocAttach> selectBizEducateDocAttachList(BizEducateDocAttach bizEducateDocAttach)
    {
        return bizEducateDocAttachMapper.selectBizEducateDocAttachList(bizEducateDocAttach);
    }

    /**
     * 新增教育资料文件
     * 
     * @param bizEducateDocAttach 教育资料文件
     * @return 结果
     */
    @Override
    public int insertBizEducateDocAttach(BizEducateDocAttach bizEducateDocAttach)
    {
        bizEducateDocAttach.setCreateTime(DateUtils.getNowDate());
        bizEducateDocAttach.setCreateBy(SecurityUtils.getUsername());
        return bizEducateDocAttachMapper.insertBizEducateDocAttach(bizEducateDocAttach);
    }

    @Override
    public int insertBizEducateDocAttachNoLogin(BizEducateDocAttach bizEducateDocAttach) {
        return bizEducateDocAttachMapper.insertBizEducateDocAttach(bizEducateDocAttach);
    }

    /**
     * 修改教育资料文件
     * 
     * @param bizEducateDocAttach 教育资料文件
     * @return 结果
     */
    @Override
    public int updateBizEducateDocAttach(BizEducateDocAttach bizEducateDocAttach)
    {
        bizEducateDocAttach.setUpdateTime(DateUtils.getNowDate());
        bizEducateDocAttach.setUpdateBy(SecurityUtils.getUsername());
        return bizEducateDocAttachMapper.updateBizEducateDocAttach(bizEducateDocAttach);
    }

    /**
     * 批量删除教育资料文件
     * 
     * @param ids 需要删除的教育资料文件主键
     * @return 结果
     */
    @Override
    public int deleteBizEducateDocAttachByIds(Long[] ids)
    {
        return bizEducateDocAttachMapper.deleteBizEducateDocAttachByIds(ids);
    }

    /**
     * 删除教育资料文件信息
     * 
     * @param id 教育资料文件主键
     * @return 结果
     */
    @Override
    public int deleteBizEducateDocAttachById(Long id)
    {
        return bizEducateDocAttachMapper.deleteBizEducateDocAttachById(id);
    }
}
